const baseUrl = "http://127.0.0.1:3000"
let { token } = wx.getStorageSync('userinfo')


const interceptor = {
    config: {
      baseURL: 'https://api.example.com', // 接口的基础路径
      header: {
        'Content-Type': 'application/json'
      }
    },
    request: (req) => {
    },
    response: (res) => {
      // 拦截响应并处理结果
      if (res.statusCode === 401) {
        wx.navigateTo({url: '/pages/login/login'})
      }
      return res
    }
  }


// 参数 "options" 从接口函数传递过来
const request = (options) => {
    return new Promise((resolve,reject) => {
    	// 拼接请求地址
        options.url = baseUrl + options.url
        wx.request({
            url: options.url,
            data: options.data?options.data:{},
            method: options.method,

            header: {
                'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'authorization':options.auth?`${token}`:''                           
            },
            success: function (res) {
                // 返回成功信息
                resolve(res)    
            },
            fail: function (error) {
                // 返回错误信息
                console.log('请求失败');
                reject(error)
            }
        })
    })
}
export default request
